﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Collections.Generic;
using OF.Lib.DataAccess;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections.ObjectModel;
using System.Dynamic;
using OF.Lib.Utility;

namespace UnitTestProj
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestDDTM1()
        {
            using (DDTM ddtm = new DDTM())
            {
                try
                {
                    //创建顾客,在DB SERVER1上
                    int sysNo = DAO.SO_Insert<Customer, int>(new
                    {
                        Name = "SingleObject2DDTM",
                        LoginId = "so1DDTM",
                        CommonStatus = CommonStatus.Actived
                    }, "SysNo", ddtm);

                    //更新书的状态,在DB SERVER2上
                    DAO.SO_Update<Book>(new { CommonStatus = CommonStatus.DeActived }, null,
                    f => f.Author.DB_Like("Jin"), ddtm);
                    ddtm.Commit();
                }
                catch
                {
                    ddtm.Rollback();
                }
            }
        }


        [TestMethod]
        public void TestDDTM()
        {
            using (DDTM ddtm = new DDTM())
            {
                try
                {
                    //创建顾客,在DB SERVER1上
                    int sysNo = DAO.SO_Insert<Customer, int>(new
                    {
                        Name = "SingleObject2DDTM",
                        LoginId = "so1DDTM",
                        CommonStatus = CommonStatus.Actived
                    }, "SysNo", ddtm);

                    //更新书的状态,在DB SERVER2上
                    DAO.SO_Update<Book>(new { CommonStatus = CommonStatus.DeActived }, null,
                    f => f.Author.DB_Like("Jin"), ddtm);
                    ddtm.Commit();
                }
                catch
                {
                    ddtm.Rollback();
                }
            }
        }

        [TestMethod]
        public void TestSingleObject()
        {
            int count = DAO.SO_Insert<Customer>(new Customer()
            {
                Name = "SingleObject1-1",
                LoginId = "so1",
                CommonStatus = CommonStatus.Actived
            }
            , "SysNo");

            int sysNo = DAO.SO_Insert<Customer, int>(new
            {
                Name = "SingleObject2",
                LoginId = "so1",
                CommonStatus = CommonStatus.Actived
            }, "SysNo");

            //int sysno = DAO.SO_Insert<Customer,int>(new Customer() { Name = "SingleObject2-1", LoginId = "so2", CommonStatus = CommonStatus.Actived }
            //,"SysNo");

            //Console.Write(sysno);

            DAO.SO_Update<Customer>(new { CommonStatus = CommonStatus.DeActived }, null,
                f => f.SysNo == 1 && (f.CommonStatus == CommonStatus.Actived || f.Name == "Jin"));
            DAO.SO_Update<Customer>(new { CommonStatus = CommonStatus.DeActived }, null, f => f.LoginId == "uncleqin");
            //DAO.SO_Update<Customer>(new Customer() { CommonStatus = CommonStatus.Actived,Memo="test so_update customer" },
            //    "SysNo,loginid,name,indate",
            //    f => f.SysNo == 1 );
            Customer c = DAO.SO_Load<Customer>(f => f.SysNo == 1);
            int d = DAO.SO_Delete<Customer>(f => f.SysNo == 5);
        }

        [TestMethod]
        public void TestInsertUpdate()
        {
            //Insert示例
            int sysno = DAO.ExecuteScalar<int>("CreateCustomer", new Customer() { Name = "张三", LoginId = "zhangsan", CommonStatus = CommonStatus.Actived });
            //Load示例
            Customer c = DAO.ExecuteEntity<Customer>("LoadCustomer", new { SysNo = sysno });
            Console.Write(c.Name + "--" + c.CommonStatus.ToString());
            //Update示例
            DAO.Execute("UpdateCustomerStatus", new { CommonStatus = CommonStatus.DeActived, SysNo = sysno });
        }

        [TestMethod]
        public void TestGetRefData1_1()
        {

            //1-1的单个获取
            Review rev2 = DAO.ExecuteEntity<Review, Book, Review>("GetReview",
                (review, book) => { review.RefBook = book; return review; }, new { sysno = 4 }, null, "SysNo");
            Console.Write(rev2.RefBook.Title);


            //1-1的列表获取
            List<Review> list2 = DAO.ExecuteEntityList<Review, Book, Review>("GetReviewList",
                (review, book) => { review.RefBook = book; return review; },
                new { SysNoes = new int[] { 4, 5 } },
                null, "SysNo");
            Console.Write(list2.Count);
        }

        [TestMethod]
        public void TestGetRefData1_N()
        {
            //1-N的单个获取，示例中，一个Customer有多个Review，也有多本Book
            Customer ctemp = null;
            Customer c1 = DAO.ExecuteEntity<Customer, Book, Review, Customer>("GetCustomerBooksAndReviews"
                , (customer, book, review) =>
                {
                    if (ctemp == null || ctemp.SysNo != customer.SysNo) ctemp = customer;
                    if (book != null) ctemp.RefBooks.Add(book);
                    if (review != null) ctemp.RefReviews.Add(review);
                    return ctemp;
                }
                , new { sysno = 1 }, null, "SysNo");
            Console.Write(c1.Name);

            //1-N的列表获取
            ctemp = null;
            List<Customer> clist = DAO.ExecuteEntityList<Customer, Review, Book, Customer>("GetCustomerBooksAndReviewsList"
               , (customer, review, book) =>
               {
                   if (ctemp == null || ctemp.SysNo != customer.SysNo) ctemp = customer;
                   if (book != null) ctemp.RefBooks.Add(book);
                   if (review != null) ctemp.RefReviews.Add(review);
                   return ctemp;
               }
               , new { SysNoes = new int[] { 1, 2, 3 } }, null, "SysNo");
            Console.Write(clist.Count);
        }

        [TestMethod]
        public void TestGetMultiple()
        {
            //多个返回数据获取
            var dg = DAO.ExecuteMultiple("GetMultipleObject", new { CustomerSysNo = 1, InDate = "2016-2-1 0:0:0" });
            var customer = dg.Read<Customer>().FirstOrDefault();
            var reviews = dg.Read<Review>().ToList();
            var books = dg.Read<Book>().ToList();
            Console.Write("OK");
        }

        [TestMethod]
        public void TestPagedQuery()
        {
            //分页查询示例
            QF_Review filter = new QF_Review() { LoginId = "Jin%", SysNoList = new List<int>() { 1, 2, 3 }, PageIndex = 0, PageSize = 10 };
            QueryResult<QR_Review> result = DAO.PagedQuery<QR_Review>("QueryCustomerReview", filter);
            int count = result.TotalCount;


        }


        [TestMethod]
        public void TestSingleEntity()
        {
            DAO.SO_Load<Customer>(f => f.SysNo == 1);
        }

        [TestMethod]
        public void TestExpressionParse()
        {
            //dynamic dyObj = new ExpandoObject();
            //var dic = (IDictionary<string, object>)dyObj;
            //dic.Add("Name", "aaa");
            //dic.Add("Price", 24.35);
            //Console.WriteLine(dyObj.Name);
            //Console.WriteLine(dyObj.Price);
            int[] sysnoes = new int[] { 1, 2, 3 };
            //f.SysNo.DB_In(new int[] { 1, 2, 3 })  && f.CommonStatus == CommonStatus.Actived
            string mm = "用户";
            Customer cc = new Customer() { SysNo = 25, Memo = "用户" };
            int len = 3;
            List<Customer> list = DAO.SO_GetList<Customer>(f => f.Memo.DB_Like(cc.Memo) && f.LoginId.DB_Length() == len, true, "SysNo ASC");
            Console.Write(list.Count);


            //Customer cc = new Customer() { SysNo = 25 };
            //string cp = "XX1231232123";

            ////|| c.LoginId.DB_Like("ABC")

            ////Expression<Func<Customer, bool>> where = c => c.Name == "Admin"
            //////|| c.SysNo.DB_In<int>(new List<int> { 1, 2, 3 })
            //// && c.CommonStatus == CommonStatus.Actived
            ////&& (c.SysNo <= cc.SysNo || c.Phone == cp); 
            //Expression<Func<Customer, bool>> where = c => c.SysNo.DB_In<int>(new int[] { 1, 2, 3 })
            //    && c.Name == "Admin"
            //    && c.CommonStatus == CommonStatus.Actived
            //    && (c.SysNo <= cc.SysNo || c.Phone == cp);

            //SOWhereBuilder<Customer> cb = new SOWhereBuilder<Customer>();
            //cb.Build(where.Body);
            //string strSQL = cb.Condition;
            //dynamic paramObj = cb.ParameterObject;
            //var sysnoes = paramObj.SYSNO_DB_IN_ARRAY;



            // Expression<Func<Customer, object>> newC = f => new { f.Name, f.InDate,f.LoginId };
            //foreach (var item in ((NewExpression)newC.Body).Arguments)
            //{
            //    string f = item.ToString();
            //    string s = item.GetType().Name;
            //}


        }



    }
}
